package com.lou1052.data.service.db;

import com.lou1052.data.entity.db.EntityObject;
import com.lou1052.data.query.IQuery;
import com.lou1052.data.query.page.IPage;
import com.lou1052.data.query.page.IPageResult;
import com.lou1052.data.repositories.db.jpa.BaseRepositorySupport;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;

/**
 * Created with IntelliJ IDEA.
 * User: liuqing
 * Date: 2016/1/5
 * Time: 22:58
 * To change this template use File | Settings | File Templates.
 */
public abstract class GenericEntityServiceSupport<ID extends Serializable,E extends EntityObject<ID>,D extends BaseRepositorySupport<ID,E>>  implements GenericEntityService<ID,E> {
    @Autowired
    protected D dao;

    @Override
    public IPageResult<E> query(IQuery query, IPage pageQuery) {
        return dao.query(query,pageQuery);
    }

    @Override
    public E find(ID id) {
        return dao.findOne(id);
    }

    @Override
    public boolean has(ID id) {
        return dao.exists(id);
    }

    @Override
    public void remove(ID id) {
        dao.delete(id);
    }

    @Override
    public void remove(E e) {
        dao.delete(e);
    }

    @Override
    public ID add(E e) {
        return dao.save(e).getId();
    }

    @Override
    public void add(Iterable<E> pos) {
        dao.save(pos);
    }

    @Override
    public void modify(E e) {
        dao.saveAndFlush(e);
    }
}
